package com.woniuxy.credit_loans.accounting_shi.service.impl;

import com.woniuxy.credit_loans.accounting_shi.service.RepaymentService;
import com.woniuxy.credit_loans.accounting_shi.vo.WeiDaVo;
import com.woniuxy.credit_loans.entity.Account;
import com.woniuxy.credit_loans.entity.Pipeline;
import com.woniuxy.credit_loans.entity.Repayment;
import com.woniuxy.credit_loans.mapper.AccountMapper;
import com.woniuxy.credit_loans.mapper.PipelineMapper;
import com.woniuxy.credit_loans.mapper.RepaymentMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class RepaymentServiceImpl implements RepaymentService {
    @Resource
    private RepaymentMapper repaymentMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private PipelineMapper pipelineMapper;
    @Override
    public List<Repayment> selectAll(String by,String name) {
        return repaymentMapper.selectAll(by,name);
    }
    @Transactional
    @Override
    public Repayment selectById(Long id) {
        Repayment repayment = repaymentMapper.selectById(id);
        Account account = accountMapper.selectByPrimaryKey(3l);
        account.setAccountRevenue(account.getAccountRevenue().add(repayment.getCurrentSum()));
        Pipeline pipeline=new Pipeline();
        pipeline.setPipelineInorout("转入");
        pipeline.setPipelineBy1(account.getAccountNo());
        pipeline.setPipelineBy2(repayment.getCurrentSum().toString());
        pipeline.setPipelineType("转账流水");
        int i = pipelineMapper.insertSelective(pipeline);
        Account account1 = accountMapper.selectAccountGongBank(id, new WeiDaVo());
        double v = 0.05;
        double d=Double.parseDouble(repayment.getCurrentInterest().toString())*v;
        double c=Double.parseDouble(repayment.getCurrentSum().toString())-d;
        account.setAccountBanlance(account.getAccountBanlance().add(BigDecimal.valueOf(d)));
        account.setAccountOutlay(account.getAccountOutlay().add(BigDecimal.valueOf(d)));
        accountMapper.updateByPrimaryKeySelective2(account);
        Pipeline pipeline2=new Pipeline();
        pipeline2.setPipelineInorout("转出");
        pipeline2.setPipelineBy1(account.getAccountNo());
        pipeline2.setPipelineBy2(String.valueOf(c));
        pipeline2.setPipelineType("转账流水");
        int i2 = pipelineMapper.insertSelective(pipeline2);
        account1.setAccountBanlance(account1.getAccountBanlance().add(BigDecimal.valueOf(c)));
        account1.setAccountRevenue(account1.getAccountRevenue().add(BigDecimal.valueOf(c)));
        accountMapper.updateByPrimaryKeySelective2(account1);
        Pipeline pipeline3=new Pipeline();
        pipeline3.setPipelineInorout("转入");
        pipeline3.setPipelineBy1(account1.getAccountNo());
        pipeline3.setPipelineBy2(String.valueOf(c));
        pipeline3.setPipelineType("转账流水");
        int i3 = pipelineMapper.insertSelective(pipeline3);
        if (repayment.getNowExpirse()+repayment.getPrepaymentPeriods()==repayment.getSumExpirse()){
            repayment.setBy1("通过");
            repayment.setNowExpirse(repayment.getNowExpirse()+repayment.getPrepaymentPeriods());
            repaymentMapper.updateByPrimaryKeySelective(repayment);
        }else {
            repayment.setBy1("未还款");
            repayment.setNowExpirse(repayment.getNowExpirse()+repayment.getPrepaymentPeriods());
            repayment.setPrepaymentPeriods(1);
            repaymentMapper.updateByPrimaryKeySelective(repayment);
        }
        return repayment;
    }
    @Transactional
    @Override
    public int updateByPrimaryKeySelective(Repayment record) {
        Account account = accountMapper.selectAccountBankAccount(Long.valueOf(record.getHeTongId()), new WeiDaVo());
        account.setAccountBanlance(account.getAccountBanlance().subtract(record.getCurrentSum()));
        accountMapper.updateByPrimaryKeySelective2(account);
        Account account1 = accountMapper.selectByPrimaryKey(3l);
        account1.setAccountBanlance(record.getCurrentSum().add(account1.getAccountBanlance()));
        account1.setAccountRevenue(account1.getAccountRevenue().add(record.getCurrentSum()));
        accountMapper.updateByPrimaryKeySelective2(account1);
        Pipeline pipeline=new Pipeline();
        pipeline.setPipelineInorout("转入");
        pipeline.setPipelineBy1(account.getAccountNo());
        pipeline.setPipelineBy2(record.getCurrentSum().toString());
        pipeline.setPipelineType("转账流水");
        pipelineMapper.insertSelective(pipeline);
        return repaymentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Integer> selectInt(Repayment repayment) {
        return null;
    }

    @Override
    public Repayment selectByPrimaryKey(Long id) {
        Repayment repayment = repaymentMapper.selectByPrimaryKey(id);
        return repayment;
    }
}
